/*
 * ﻿Copyright (C) 2012-2013 NewMain Softech
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */
package com.newmainsoftech.ant.taskdefs;

import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.util.List;
import java.util.Properties;
import java.util.Set;

import junit.framework.Assert;

public class DummyAppForTestingJavaTaskExt {
	static final String JavaArgPrefix = "JavaArg:";
		public static String getJavaArgPrefix() {
			return JavaArgPrefix;
		}
	static final String JvmArgPrefix = "JvmArg:";
		public static String getJvmArgPrefix() {
			return JvmArgPrefix;
		}
	static final String SysPropPrefix = "SysProp:";
		public static String getSysPropPrefix() {
			return SysPropPrefix;
		}


	public static void main( final String... args) throws IOException {
		Properties properties = new Properties();
		StringBuffer stringBuffer = new StringBuffer();
		
		// Regarding arguments --------------------------------------------------------------------
		for( int argIndex = 0; argIndex < args.length; argIndex++) {
			String argStr = args[ argIndex];
				stringBuffer.append( 
						String.format(
								"%n#%1$cargument %2$d: \"%3$s\"",
								'\t', argIndex, argStr
								)
						);
			int equalDelimiterIndex = argStr.indexOf( "=");
				if ( equalDelimiterIndex < 0) continue; // for
			properties.setProperty( 
					DummyAppForTestingJavaTaskExt.getJavaArgPrefix() 
					+ argStr.substring( 0, equalDelimiterIndex).trim(), 
					argStr.substring( (equalDelimiterIndex + 1)).trim());
		} // for
			System.out.println(
					String.format(
							"# Following arguments were fed: %1$s",
							stringBuffer.toString()
							)
					);
		// ----------------------------------------------------------------------------------------
			
		// Regarding system property --------------------------------------------------------------
		final Properties systemProperties = System.getProperties();
		final Set<Object> keySet = systemProperties.keySet();
		stringBuffer = new StringBuffer();
		int argCounter = 0;
		for( Object keyObj : keySet) {
			if ( !keyObj.toString().startsWith( "sysprop.")) continue; // for
			String systemPropertyValue = systemProperties.get( keyObj).toString();
				stringBuffer.append( 
						String.format(
								"%n#%1$system property %2$d: %3$s=%4$s",
								'\t', argCounter++, keySet.toString(), systemPropertyValue
								)
						);
			properties.setProperty( 
					DummyAppForTestingJavaTaskExt.getSysPropPrefix()  
					+ keyObj.toString(), 
					systemPropertyValue);
			
		} // for
			System.out.println(
					String.format(
							"# Following system properties were fed: %1$s",
							stringBuffer.toString()
							)
					);
		// ----------------------------------------------------------------------------------------
		
		// Regarding JVM arguments ----------------------------------------------------------------
		RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
		List<String> jvmArgList = runtimeMXBean.getInputArguments();
		stringBuffer = new StringBuffer();
		argCounter = 0;
		for( String jvmArg : jvmArgList) {
			stringBuffer.append( 
					String.format(
							"%n#%1$cJVM argument %2$d: \"%3$s\"",
							'\t', argCounter++, jvmArg
							)
					);
			int equalDelimiterIndex = jvmArg.indexOf( "=");
				if ( equalDelimiterIndex < 0) continue; // for
			properties.setProperty( 
					DummyAppForTestingJavaTaskExt.getJvmArgPrefix() 
					+ jvmArg.substring( 0, equalDelimiterIndex).trim(), 
					jvmArg.substring( (equalDelimiterIndex + 1)).trim());
		} // for
			System.out.println(
					String.format(
							"# Following JVM arguments were fed: %1$s",
							stringBuffer.toString()
							)
					);
		// ----------------------------------------------------------------------------------------
		
		properties.store( System.out, null);
	}
}
